<template>
	<view class="tabBlock" v-if="list.length > 0">
		<!-- <scroll-view class="learning_scroll_view" scroll-x>
			<view class="learning_view">
				<view v-for="(langeItem,langeIndex) in list" :key="langeIndex" class="language_item">
					<view :class="['onelist-hidden',langeIndex==index?'active':'']" @tap.stop="bindIndex(langeIndex)">
						{{langeItem.title}}
						<text v-if="langeIndex==index" class="active_border"></text>
					</view>
				</view>
			</view>
		</scroll-view> -->
		<scroll-view scroll-x="true" scroll-with-animation :scroll-left="tabsScrollLeft" @scroll="scroll">
			<view class="tab" id="tab_list">
				<view v-for="(langeItem,langeIndex) in list" 
					  :key="langeIndex"
					  :class="['tab__item', {'tab__item--active': index === langeIndex}]"
					  :style="{color: (index === langeIndex ? `${itemColor}`: '')}"
					  id="tab_item"
					  @click="bindIndex(langeIndex)"
				>
					<view :class="['tab__item-title', {'tab__item--active': index === langeIndex}]">
						<slot name="title" :title="langeItem.title"></slot>
					</view>
					<text v-if="!showTitleSlot" class="tab__item-title" id="tab__item-title_width">
						{{langeItem.title}}
					</text>
				</view>
			</view>
			<view class="tab__line" 
				  :style="{backgroundImage: 'linear-gradient(270deg, #7f4aff 0%, #a977ff 100%)', width: lineStyle.width, transform: lineStyle.transform,transitionDuration: lineStyle.transitionDuration}">
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		props:{
			list:{
				type:Array,
				default:[]
			},
			index:{
				type:[Number,String],
				default:0
			}
		},
		data() {
			return {
				lineStyle: {},
				scrollLeft: 0,
				tabsScrollLeft: 0,
				duration: 0.3
			};
		},
		computed: {
			showTitleSlot() {
				return this.$scopedSlots.title
			}
		},
		watch: {
			list() {
				// console.log("--------------------------")
				this.setTabList()
			},
			index() {
				this.index = this.index
				this.setTabList()
			}
		},
		methods:{
			setTabList() {
				this.$nextTick(()=>{
					if(this.list.length > 0) {
						this.setLine()
						this.scrollIntoView()
					}
				})
			},
			scrollIntoView() {  // item滚动
				let lineLeft = 0;
				this.getElementData('#tab_list', (data)=> {
					let list = data[0]
					this.getElementData(`#tab_item`, (data)=> {
						let el = data[this.index]
						// lineLeft = el.width * (this.index + 0.5) - list.width / 2 - this.scrollLeft
						lineLeft = el.width / 2 + (-list.left) + el.left - list.width / 2 - this.scrollLeft
						this.tabsScrollLeft = this.scrollLeft + lineLeft
					})
				})
			},
			getElementData(el, callback){
				uni.createSelectorQuery().in(this).selectAll(el).boundingClientRect().exec((data) => {
					callback(data[0]);
				});
			},
			setLine() {
				let lineWidth = 0, lineLeft = 0
				this.getElementData(`#tab_item`, (data)=> {
					let el = data[this.index]
					// lineWidth = el.width / 2
					// lineLeft = el.width * (this.index + 0.5)  // 此种只能针对每个item长度一致的
					lineLeft = el.width / 2 + (-data[0].left) + el.left
					this.getElementData(`#tab__item-title_width`, (data2)=> {
						let el2 = data2[this.index]
						lineWidth = el2.width 
						console.log(el2.width)
						this.lineStyle = {
							width: `${lineWidth}px`,
							transform: `translateX(${lineLeft}px) translateX(-50%)`,
							transitionDuration: `${this.duration}s`
						};
					})
					// this.lineStyle = {
					// 	// width: `${lineWidth}px`,
					// 	transform: `translateX(${lineLeft}px) translateX(-50%)`,
					// 	transitionDuration: `${this.duration}s`
					// };
				})
			},
			bindIndex(index){
				this.$emit('bindTitleBar',index);
			},
			scroll(e) {
				this.scrollLeft = e.detail.scrollLeft;
			}
		}
	}
</script>

<style lang="scss">
	.tabBlock {
		position: relative;
		background: #fff;
		.tab {
			position: relative;
			display: flex;
			font-size: 28rpx;
			padding-bottom: 15rpx;
			white-space: nowrap;
			z-index: 11;
			&__item {
				flex: 1;
				// width: 30%;
				text-align: center;
				// line-height: 90rpx;
				color: $uni-text-color;
				&--active {
					color: #252748;
					font-size: 32rpx;
					font-weight: 600;
				}
				&-title {
					margin: 0 40rpx;
				}
			}
		}
		.tab__line {
			display: block;
			height:14rpx;
			position: absolute;
			bottom: 26rpx;
			left: 0;
			z-index: 1;
			border-radius: 3rpx;
			position: relative;
			background: $uni-color-primary;
		}
	}
</style>

<!-- <style lang="less">
	.learning_scroll_view{
		background-color: #fff;
		margin:12rpx 0rpx 12rpx;
		width: 750rpx;
		height:48rpx;
		padding: 20rpx 0rpx 8rpx;
		.learning_view{
			display: flex;
			flex-wrap: nowrap;
			height: 40rpx;
		}
		.language_item{
			display: inline-block;
			&:first-child{
				padding-left: 48rpx;
			}
			&:last-child{
				padding-right: 48rpx;
			}
			.onelist-hidden{
				display: block;
				font-size: 28rpx;
				line-height: 40rpx;
				position: relative;
				// &.active{
					color: #252748;
				// }
				&:not(.active){
					// color: #252748;
					opacity: 0.6;
				}
			}
			&:not(:last-child) .onelist-hidden{
				margin-right: 72rpx;
			}
			.active_border{
				display: block;
				margin-left: calc(50% - 14rpx);
				width: 28rpx;
				height: 4rpx;
				background-image: linear-gradient(180deg, #a977ff 0%, #7f4aff 100%);
				border-radius: 4rpx;
			}
		}
	}
</style>
 -->